from PySide2.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QPushButton
from PySide2.QtCore import Signal
import math

from tool import util, storage, dbutil

from custom.PageNumber import PageNumber
from custom.SearchView import SearchView
from custom.VideoListWidget import VideoListWidget
from model import Video
from custom.Runthread import OnUploadVideo

class TabTXVideo(QWidget):

    updateUploador_signal = Signal()

    def __init__(self):
        super(TabTXVideo, self).__init__()
        self.data = []
        self.search = ""
        self.page = 1
        self.page_size = 10
        self.total = 0
        self.setupUi()
        searchHistorys = ["王者荣耀", "英雄联盟", "吃鸡", "搞笑"]
        self.searchView.setSearchHistorys(searchHistorys)
        self.setupData()

    def setupUi(self):
        self.layout = QHBoxLayout(self)
        
        self.leftLayout = QVBoxLayout()
        self.rightLayout = QVBoxLayout()
        self.layout.addLayout(self.leftLayout)
        self.layout.addLayout(self.rightLayout)

        self.searchView = SearchView()
        self.searchView.search_signal.connect(self.search_controller)
        self.searchView.today_signal.connect(self.today_controller)
        self.listWidget = VideoListWidget()
        self.listWidget.clickedImage.connect(self.onClickedImage)
        self.listWidget.updateUploador_signal.connect(self.updateUploador_signal)

        self.pageNumber = PageNumber()
        self.pageNumber.control_signal.connect(self.page_controller)

        self.leftLayout.addWidget(self.searchView)
        self.leftLayout.addWidget(self.listWidget)
        self.leftLayout.addWidget(self.pageNumber)

        self.leftLayout.setStretchFactor(self.listWidget, 1)

        self.pushButton = QPushButton("上传")
        self.pushButton.clicked.connect(self.onClickUpload)

        self.selectWidget = VideoListWidget()
        self.selectWidget.clickedImage.connect(self.onClickedImage_select)
        self.selectWidget.updateUploador_signal.connect(self.updateUploador_signal)

        # self.rightLayout.addWidget(self.pushButton)
        self.rightLayout.addWidget(self.selectWidget)


    def setupData(self):
        total = dbutil.fetchVideoNumber(2)
        page = math.ceil(int(total) / self.page_size)
        self.pageNumber.setPage(page)

        selectedVideos = dbutil.fetchSelectedVideos()
        self.selectWidget.reloadData(selectedVideos)

    def page_controller(self, number):
        self.page = number
        self.request()

    def search_controller(self, text):
        if self.search != text:
            self.page = 1
            self.pageNumber.reset()
        self.search = text
        self.request()

    def today_controller(self):
        self.page = 1
        self.pageNumber.reset()
        self.data, self.total = dbutil.fetchTodayVideo(
            self.page, self.page_size, "tx")
        self.listWidget.reloadData(self.data)
        if (self.page == 1):
            page = math.ceil(int(self.total) / self.page_size)
            self.pageNumber.setPage(page)

    def request(self):
        self.data, self.total = dbutil.fetchVideo(
            self.page, self.page_size, "tx", self.search)
        self.listWidget.reloadData(self.data)
        if (self.page == 1):
            page = math.ceil(int(self.total) / self.page_size)
            self.pageNumber.setPage(page)

    def onClickedImage(self, index, video):
        dbutil.updateVideo(video.id, {"state": "1"})
        self.selectWidget.add_item(video)

    def onClickedImage_select(self, index, video):
        dbutil.updateVideo(video.id, {"state": "0"})
        self.selectWidget.remove_item(index)

    def onClickUpload(self):
        # TODO 遍历下载

        self.thread = OnUploadVideo(self.selectWidget.list)
        self.thread.finishsignal.connect(self.onUploadVideoFinish)
        self.thread.start()

    def onUploadVideoFinish(self):
        print("onUploadVideoFinish")

